#!/usr/bin/env python
def count_votes(csvfile, candidate):
    '''
Purpose: The function will load in one of these vote files and return a list containing 
the counts of first, second, and third-place votes for the given candidate.
csvfile: The candidate csv file.
candidate: The candidate types(first_choice,second_choice,third_choice)
Return: The candidate list. 
'''
    name1 = 0
    name2 = 0
    name3 = 0
    try:
        file = open(csvfile, 'r')
        for line in file.readlines()[1:]:
            candidate_list = line.strip().split(',')
            if len(candidate_list) == 4:
                if candidate_list[1] == candidate:
                    name1 += 1
                if candidate_list[2] == candidate:
                    name2 += 1
                if candidate_list[3] == candidate:
                    name3 += 1
            if len(candidate_list) == 3:
                if candidate_list[1] == candidate:
                    name1 += 1
                    name2 += 1
                    name3 = 0
            if len(candidate_list) == 2:
                if candidate_list[1] == candidate:
                    name1 += 1
                    name2 = 0
                    name3 = 0
        file.close()
    
    except FileNotFoundError:
        return "#Note: None is return here"
    count_list = [name1, name2, name3]
    return count_list
def remove_candidate(csvfilein, csvfileout, candidate):
'''
Purpose: The function will remove a particular candidate from contention by removing their name from any votes.
csvfilein: The oringal candidate file.
csvfileout: The output csv file.
candidate: The candidate that will be removed.
Return: The number of removed candidate. 
'''
    number = 0
    with open(csvfilein, 'r') as fin:
        lines = fin.readlines()
    with open(csvfileout, 'w') as fout:
        for line in lines:
            line_list = line.strip().split(',')
            if len(line_list) == 1:
                continue
            else:
                for name in line_list:
                    if name == candidate:
                        line_list.remove(candidate)
                        number += 1
            fout.writelines(','.join(line_list)+'\n')       
    return number
def ranked_choice_full(csvfile, candidate_list):
    '''
Purpose: This function takes a csvfile name and a list of candidate names as parameters. 
The function should return the winner of the election, according to the ranked-choice voting process.
csvfile: A input csv file.
candidate_list: A candidate list.
Return: The final selection. 
'''
    rounds = 0
    most_votes = 0.0 
    most_one =''
    while most_votes < 0.5:
        
        candidates = []
        if rounds == 0:
            for candidate in candidate_list:
                candidates.append(count_votes(csvfile, candidate)[0])
            fewest_one = candidate_list[candidates.index(min(candidates))]
            most_votes = max(candidates)/sum(candidates)
            number = remove_candidate(csvfile, 'round%d_%s'%(rounds+1, csvfile), fewest_one)
            candidate_list.remove(fewest_one)
            print('In round 1,',fewest_one,' is eliminated')
        else:
            for candidate in candidate_list:
                candidates.append(count_votes('round%d_%s'%(rounds, csvfile), candidate)[0])
            fewest_one = candidate_list[candidates.index(min(candidates))]
            most_one = candidate_list[candidates.index(max(candidates))]
            most_votes = max(candidates)/sum(candidates)
            number = remove_candidate('round%d_%s'%(rounds, csvfile), 'round%d_%s'%(rounds+1, csvfile), fewest_one)
            candidate_list.remove(fewest_one)
            print('In round %d,'%(rounds),fewest_one,' is eliminated')
        rounds += 1
    return most_one

